package com.cs.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cs.entity.Course;
import com.cs.entity.Grade;
import com.cs.entity.Student;
import com.cs.entity.Teacher;
import com.cs.service.StudentServiceImpl;

@Controller
public class StudentController {
	@Resource
	private StudentServiceImpl sImpl;
	
	//学生登录
	@RequestMapping("stulogin")
	public Object studentLogin(HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(username);
		Student student = this.sImpl.studentLogin(username, password);
		PrintWriter out = response.getWriter();
		if (student!=null) {
			request.getSession().setAttribute("student", student);
			out.print("<script language='javascript'>alert('登录成功');window.location.href='stuindex.jsp'</script>");
		}
		out.print("<script language='javascript'>alert('用户名或密码错误');window.location.href='login.jsp'</script>");
		return out;
	}
	//查询个人信息（根据id）
	@RequestMapping("findstudent")
	public String findTeacherById(HttpServletRequest request,Model model){
		Student student = (Student) request.getSession().getAttribute("student");
		Integer id = student.getId();
		Student student2 = this.sImpl.findStudentById(id);
		model.addAttribute("findStudent", student2);
		return "/stuinfo.jsp";
	}
	//修改个人信息
	@RequestMapping("updatestu")
	public Object updatestu(Student student, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		boolean b = this.sImpl.updateStudent(student);
		PrintWriter out = response.getWriter();
		if (b) {
			out.print("<script language='javascript'>alert('修改成功');window.location.href='findstudent'</script>");
		}
		out.print("<script language='javascript'>alert('修改失败');window.location.href='findstudent'</script>");
		return out;
	}
	//修改密码
	@RequestMapping("toupdatestupwd")
	public String toupdatestupwd(@RequestParam("id") Integer id,Model model){
		model.addAttribute("sid", id);
		return "/updatestupwd.jsp";
	}
	@RequestMapping("updatestupwd")
	public Object updatestupwd(@RequestParam("id") Integer id,HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String oldpwd = request.getParameter("oldpwd");
		String newpwd = request.getParameter("newpwd");
		String confirmpwd = request.getParameter("confirmpwd");
		System.out.println(newpwd+"+"+confirmpwd);
		Student student = this.sImpl.findStudentById(id);
		PrintWriter writer = response.getWriter();
		//检查原密码是否正确
		if (oldpwd.equals(student.getSpassword())) {
			//判断两次输入的密码是否一致
			if (newpwd.equals(confirmpwd)) {
				student.setSpassword(newpwd);
				this.sImpl.updateStudent(student);
				writer.print("<script language='javascript'>alert('修改成功');window.location.href='stuindex.jsp'</script>");
			}
			writer.print("<script language='javascript'>alert('两次输入的密码不一致');window.location.href='updatestupwd.jsp'</script>");
		}
		writer.print("<script language='javascript'>alert('原密码错误');window.location.href='updatestupwd.jsp'</script>");
		return writer;
		
	}
	//查询学生可选课程
	@RequestMapping("findoptioncou")
	public String findOptionalCourses(HttpServletRequest request,Model model){
		Student student = (Student) request.getSession().getAttribute("student");
		List<Course> list = this.sImpl.findOptionalCourses(student.getSpecialities());
		model.addAttribute("findOptionalCourses", list);
		return "/couselection.jsp";
	}
	//选课功能
	@RequestMapping("elective")
	public Object electiveCourse(@RequestParam("cid") Integer cid,@RequestParam("sid") Integer sid
			,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter writer = response.getWriter();
		Course course = this.sImpl.findCourseById(cid);
		Student student = this.sImpl.findStudentById(sid);
		if (course.getNumber()>0) {
			this.sImpl.electiveCourse(student, course);
			writer.print("<script language='javascript'>alert('选课成功');window.location.href='findoptioncou'</script>");
		}
		writer.print("<script language='javascript'>alert('选课失败，该课程人数已满！');window.location.href='findoptioncou'</script>");
		return writer;
	}
	//查看成绩
	@RequestMapping("selectgrade")
	public String selectGrade(@RequestParam("id") Integer id,Model model){
		List<Grade> gList = this.sImpl.selectGrade(id);
		model.addAttribute("gList", gList);
		return "/selectgrade.jsp";
	}
	//查看已选课程
	@RequestMapping("mycourse")
	public String mycourse(@RequestParam("id") Integer id,Model model){
		Student student = this.sImpl.findStudentById(id);
		model.addAttribute("mycourse", student.getCourse());
		return "/mycourse.jsp";
	}

}
